package cn.yshujia.controller;

import cn.yshujia.domain.dto.PageDTO;
import cn.yshujia.domain.entity.Resources;
import cn.yshujia.domain.vo.ApiVO;
import cn.yshujia.domain.vo.PageVO;
import cn.yshujia.domain.vo.ResourcesVO;
import cn.yshujia.service.impl.ResourcesServiceImpl;
import com.alibaba.fastjson2.JSON;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author YShuJia
 * @since 2024/4/23
 */
@RestController
@Tag (name = "Resources", description = "资源Api")
public class ResourcesController {
	
	@Resource
	private ResourcesServiceImpl service;
	
	@GetMapping ("/resources/page")
	@Operation (summary = "根据查询条件获取资源列表")
	public ApiVO<PageVO<ResourcesVO>> list(@RequestParam String dto) {
		PageDTO dtoPage = JSON.parseObject(dto, PageDTO.class);
		if (null == dtoPage) {
			return ApiVO.error();
		}
		return ApiVO.success(service.selectWallPage(dtoPage));
	}
	
	@GetMapping ("/admin/resources/page")
	@Operation (summary = "admin 根据查询条件获取资源分页")
	@PreAuthorize ("@sys.hasOnePermission('resources:all')")
	public ApiVO<PageVO<ResourcesVO>> adminList(@RequestParam String dto) {
		PageDTO dtoPage = JSON.parseObject(dto, PageDTO.class);
		if (null == dtoPage) {
			return ApiVO.error();
		}
		return ApiVO.success(service.adminPage(dtoPage));
	}
	
	@PostMapping ("/admin/resources/insert")
	@Operation (summary = "admin 添加资源，批量时以 Resources 传递的信息为基础")
	@PreAuthorize ("@sys.hasOnePermission('resources:all')")
	public ApiVO<Boolean> insert(@RequestParam ("files") MultipartFile[] files, Resources resources) {
		if (files == null || files.length == 0 || resources == null) {
			return ApiVO.error();
		}
		boolean bool = service.insert(files, resources);
		if (bool) {
			return ApiVO.success(true);
		}
		return ApiVO.error();
	}
	
	@PutMapping ("/admin/resources/update")
	@Operation (summary = "admin 更新资源")
	@PreAuthorize ("@sys.hasOnePermission('resources:all')")
	public ApiVO<Boolean> update(@RequestBody Resources resources) {
		boolean result = service.update(resources);
		if (result) {
			return ApiVO.success(true);
		}
		return ApiVO.error();
	}
	
	@DeleteMapping ("/admin/resources/delete")
	@Operation (summary = "admin 删除资源")
	@PreAuthorize ("@sys.hasOnePermission('resources:all')")
	public ApiVO<Boolean> remove(@RequestBody List<Long> ids) {
		boolean result = service.remove(ids);
		if (result) {
			return ApiVO.success(true);
		}
		return ApiVO.error();
	}
}
